package com.our.sys.project1.service;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.our.set.service.HzService;
import com.our.sys.project1.dao.HighValueMaterialDetailDao;
import com.our.sys.project1.dao.HighValueMaterialTypeDao;
import com.our.sys.project1.entity.HighValueMaterialDetail;
import com.our.sys.project1.entity.HighValueMaterialType;

@Service
@Transactional
public class HighValueMaterialTypeService {

    private static Logger logger = LoggerFactory.getLogger(HighValueMaterialTypeService.class);

    @Autowired
    private HighValueMaterialTypeDao highValueMaterialTypeDao;

    @Autowired
    private HighValueMaterialDetailDao highValueMaterialDetailDao;

    @Autowired
    private HzService hzService;

    @Transactional(readOnly = true)
    public Page<HighValueMaterialType> queryS(Page<HighValueMaterialType> page, List<PropertyFilter> filters) {
        Page<HighValueMaterialType> curPage = highValueMaterialTypeDao.findPage(page, filters);
        return curPage;
    }

    @Transactional(readOnly = true)
    public Page<HighValueMaterialDetail> queryD(Page<HighValueMaterialDetail> page, List<PropertyFilter> filters) {
        Page<HighValueMaterialDetail> curPage = highValueMaterialDetailDao.findPage(page, filters);
        return curPage;
    }

    @Transactional(readOnly = true)
    public HighValueMaterialType querySById(Long id) {
        return highValueMaterialTypeDao.findOneBy("id", id);
    }

    @Transactional(readOnly = true)
    public HighValueMaterialDetail queryDById(Long id) {
        return highValueMaterialDetailDao.findOneBy("id", id);
    }

    @Transactional(readOnly = true)
    private Long queryCountByName(String name) {
        Long result = highValueMaterialTypeDao.findUnique(
                "select count(a) from HighValueMaterialType a where a.name = ?", name);
        if (null == result) {
            return 0L;
        } else {
            return result;
        }
    }

    @Transactional(readOnly = true)
    private Long queryCountByNameAndSid(String name, Long sId) {
        Long result = highValueMaterialDetailDao
                .findUnique(
                        "select count(b) from HighValueMaterialType a,HighValueMaterialDetail b where b.name = ? and a.id = ? and a.id = b.sId",
                        name, sId);
        if (null == result) {
            return 0L;
        } else {
            return result;
        }
    }

    public void saveS(HighValueMaterialType highValueMaterialType) throws Exception {
        try {
            Map<String, String> map = hzService.getMemo(highValueMaterialType.getName());
            highValueMaterialType.setPy(map.get("py"));
            highValueMaterialType.setWb(map.get("wb"));
            if (null != highValueMaterialType.getId()) {
                HighValueMaterialType getHighValueMaterialType = this.querySById(highValueMaterialType.getId());

                if (queryCountByName(getHighValueMaterialType.getName()).compareTo(1L) > 0) {
                    throw new Exception("已经存在这个高值耗材类别！");
                }

                getHighValueMaterialType.setName(highValueMaterialType.getName());
                getHighValueMaterialType.setPy(highValueMaterialType.getPy());
                getHighValueMaterialType.setWb(highValueMaterialType.getWb());
                getHighValueMaterialType.setState(highValueMaterialType.getState());
                highValueMaterialTypeDao.save(getHighValueMaterialType);
            } else {

                if (queryCountByName(highValueMaterialType.getName()).compareTo(0L) > 0) {
                    throw new Exception("已经存在这个高值耗材类别！");
                }
                highValueMaterialTypeDao.save(highValueMaterialType);
            }

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }

    public void saveD(HighValueMaterialDetail highValueMaterialDetail) throws Exception {
        try {
            Map<String, String> map = hzService.getMemo(highValueMaterialDetail.getName());
            highValueMaterialDetail.setPy(map.get("py"));
            highValueMaterialDetail.setWb(map.get("wb"));
            if (null != highValueMaterialDetail.getId()) {
                HighValueMaterialDetail getHighValueMaterialDetail = this.queryDById(highValueMaterialDetail.getId());

                if (queryCountByNameAndSid(getHighValueMaterialDetail.getName(), highValueMaterialDetail.getsId()).compareTo(1L) > 0) {
                    throw new Exception("已经存在这个高值耗材！");
                }

                getHighValueMaterialDetail.setName(highValueMaterialDetail.getName());
                getHighValueMaterialDetail.setPy(highValueMaterialDetail.getPy());
                getHighValueMaterialDetail.setWb(highValueMaterialDetail.getWb());
                getHighValueMaterialDetail.setsId(highValueMaterialDetail.getsId());
                getHighValueMaterialDetail.setCode(highValueMaterialDetail.getCode());
                getHighValueMaterialDetail.setConsum(highValueMaterialDetail.getConsum());
                getHighValueMaterialDetail.setMaterialName(highValueMaterialDetail.getMaterialName());
                getHighValueMaterialDetail.setPrice(highValueMaterialDetail.getPrice());
                getHighValueMaterialDetail.setSpec(highValueMaterialDetail.getSpec());
                getHighValueMaterialDetail.setType(highValueMaterialDetail.getType());
                getHighValueMaterialDetail.setUnit(highValueMaterialDetail.getUnit());
                highValueMaterialDetailDao.save(getHighValueMaterialDetail);
            } else {

                if (queryCountByNameAndSid(highValueMaterialDetail.getName(), highValueMaterialDetail.getsId()).compareTo(0L) > 0) {
                    throw new Exception("已经存在这个高值耗材！");
                }
                highValueMaterialDetailDao.save(highValueMaterialDetail);
            }

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }

    public void deleteS(Long id) throws Exception {
        try {
            List<HighValueMaterialDetail> list = highValueMaterialDetailDao.find(
                    "from HighValueMaterialDetail where sId = ?", id);
            if (null != list && list.size() > 0) {
                throw new Exception("这个卫生材料有对应子科目，不能删除！");
            } else {
                highValueMaterialTypeDao.delete(id);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }

    public void deleteD(Long id) throws Exception {
        try {
            highValueMaterialDetailDao.delete(id);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }

    public void setStateD(Long id, String state) throws Exception {
        try {
            HighValueMaterialDetail highValueMaterialDetail = this.queryDById(id);
            highValueMaterialDetailDao.save(highValueMaterialDetail);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }

    public void setStateS(Long id, String state) throws Exception {
        try {
            HighValueMaterialType highValueMaterialType = this.querySById(id);
            highValueMaterialType.setState(state);
            highValueMaterialTypeDao.save(highValueMaterialType);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }

}
